package com.example.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;
@WebFilter(urlPatterns = "/*")
public class FilterExample implements Filter {

    // 初始化方法，Web服务启动，创建Filter实例时调用，只调用一次
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
        System.out.println("Filter init ... ");
    }

    // 只要拦截到请求，就会调用，所以会调用多次
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Filter doFilter ... ");
        filterChain.doFilter(servletRequest, servletResponse);
    }
    // 销毁，Web服务器关闭时调用，只调用一次
    @Override
    public void destroy() {
        Filter.super.destroy();
        System.out.println("Filter destroy ... ");
    }
}
